/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2012        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control module to the FatFs module with a defined API.        */
/*-----------------------------------------------------------------------*/

#include "diskio.h"		/* FatFs lower layer API */
#include "spi_flash.h"

/* Definitions of physical drive number for each media */
#define SPIFLASH		0
#define USB		        1
#define SDCARD		    2

#define FLASH_SECTOR_SIZE 	512	
uint16_t	    FLASH_SECTOR_COUNT=2048*2;//扇区个数,如果换成8M也要改
#define FLASH_BLOCK_SIZE  	8     //块大小
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE drv				/* Physical drive nmuber (0..) */
)
{
	DSTATUS stat;
	int result = STA_NOINIT;
    
    switch(drv)
    {
        case SPIFLASH:
        {
            
            result = RES_OK;
        }break;
        case USB:
        {
        }break;
        case SDCARD:
        {
        }break;
    }
    
    return result;

}



/*-----------------------------------------------------------------------*/
/* Get Disk Status                                                       */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE drv		/* Physical drive nmuber (0..) */
)
{
	DSTATUS stat;
	int result;
    
    return 0;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	BYTE count		/* Number of sectors to read (1..128) */
)
{
	DRESULT res;
	int result;
    int cnt;
    
    if (!count)return RES_PARERR;
    
    switch(drv)
    {
        case SPIFLASH:
        {
            if(drv == 0)
            {
                while(count--)
                {
                    SPI_FLASH_BufferRead(buff, sector*FLASH_SECTOR_SIZE , FLASH_SECTOR_SIZE);
                    sector++;
                    buff += FLASH_SECTOR_SIZE;
                }
                return RES_OK;
            }       
        }break;
        
    }
        
    return RES_ERROR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if _USE_WRITE
DRESULT disk_write (
	BYTE drv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	BYTE count			/* Number of sectors to write (1..128) */
)
{
	DRESULT res;
	int result;
    
    int cnt;
    
    if (!count)return RES_PARERR;
    
    if(drv == 0)
    {
        switch(drv)
        {
            case SPIFLASH:
            {
                while(count--)
                {
                    SPI_Flash_Write((uint8_t*)buff, sector*FLASH_SECTOR_SIZE , FLASH_SECTOR_SIZE);
                    sector++;
                    buff += FLASH_SECTOR_SIZE;                   
                }
                return RES_OK;
            }break;
        }

    }
    return RES_ERROR;
}
#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

#if _USE_IOCTL
DRESULT disk_ioctl (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE ctrl,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res=RES_ERROR;
	int result;

    if(drv == SPIFLASH)
    {
	    switch(ctrl)
	    {
		    case CTRL_SYNC:
				res = RES_OK; 
		        break;	 
		    case GET_SECTOR_SIZE:
		        *(WORD*)buff = FLASH_SECTOR_SIZE;
		        res = RES_OK;
		        break;	 
		    case GET_BLOCK_SIZE:
		        *(WORD*)buff = FLASH_BLOCK_SIZE;
		        res = RES_OK;
		        break;	 
		    case GET_SECTOR_COUNT:
		        *(DWORD*)buff = FLASH_SECTOR_COUNT;
		        res = RES_OK;
		        break;
		    default:
		        res = RES_PARERR;
		        break;
	    }
    }
    
    return res;
}
#endif

DWORD get_fattime (void)
{				 
	return 0;
}	

